﻿using System;
using PTiRouZ.Net.SilverLight.Api;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Runtime.Serialization;

namespace PTiRouZ.Net.SilverLight.Api.HelloTipi
{
    /// <summary>Objet correspondant à la vérification des credentials</summary>
    /// <see cref="verify_credentials"></see>
    public class verify_credentials
    {
        /// <summary>Id de l'utilisateur</summary>
        [DataMember]
        public int id { get; set; }

        /// <summary>Prénom et nom de l'utilisateur</summary>
        [DataMember]
        public string name { get; set; }

        /// <summary>Pseudonyme de l'utilisateur</summary>
        [DataMember]
        public string screen_name { get; set; }

        /// <summary>url de l'avatar du user</summary>
        /// <remarks>url valide 1h</remarks>
        [DataMember]
        public string profile_image_url { get; set; }
    }

    public delegate void VerifyCredentialsEventHandler(object sender, VerifyCredentialsEventArgs e);

    public class VerifyCredentialsEventArgs : EventArgs
    {
        public VerifyCredentialsEventArgs(verify_credentials vc)
        {
            _result = vc;
        }

        private verify_credentials _result;
        public verify_credentials result { get { return _result; } }
    }
    public partial class apitxt : api
    {

        public event VerifyCredentialsEventHandler VerifyCredentialsCompleted;

        /// <summary>Permet de se connecter au service.</summary>
        /// <returns>Retourne une structure <see cref="verify_credentials"/></returns>
        /// <example>http://apitxt.hellotipi.com/account/verify_credentials.json</example>
        public void verify_credentials()
        {
            base.APICallCompleted += new APICallCompletedEventHandler(VerifyCredentialsCallCompleted);
            GetUrlApi("account/verify_credentials.json");
        }

        void VerifyCredentialsCallCompleted(object sender, ApiOpenReadCompletedEventArgs e)
        {
            try
            {
                System.Threading.Thread.Sleep(2000);
                //if (e.Result != null)            
                VerifyCredentialsCompleted.Invoke(sender, new VerifyCredentialsEventArgs(serializeTools.Deserialize<verify_credentials>(e.Result)));
                //else
                //    VerifyCredentialsCompleted.Invoke(sender, new VerifyCredentialsEventArgs(null));
            }
            finally
            {
                base.APICallCompleted -= new APICallCompletedEventHandler(VerifyCredentialsCallCompleted);
            }
        }

    }
}
